﻿using AutoMapper;
using MediatR;
using RedDragon.Common.Domain.ProductionTasks;
using RedDragon.Common.Infrastructure.Interface;
using RedDragon.ERP.ErroCodeEnum;
using T.Common.Domain.Common;

namespace RedDragon.ERP.API.Application.Commands.ProductionTasks
{
    /// <summary>
    /// 生产任务删除Handler
    /// </summary>
    public class DeleteProductionTasksHandler : IRequestHandler<DeleteProductionTasksCommand, Result>
    {
        /// <summary>
        /// mapper
        /// </summary>
        private readonly IMapper mapper;
        /// <summary>
        /// 生产任务表仓储
        /// </summary>
        public readonly IBaseRepository<Productiontasks> productiontasksrepository;
        /// <summary>
        /// 日志
        /// </summary>
        private readonly ILogger<DeleteProductionTasksHandler> logger;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="mapper"></param>
        /// <param name="productiontasksrepository"></param>
        /// <param name="logger"></param>
        public DeleteProductionTasksHandler(IMapper mapper, IBaseRepository<Productiontasks> productiontasksrepository, ILogger<DeleteProductionTasksHandler> logger)
        {
            this.mapper = mapper;
            this.productiontasksrepository = productiontasksrepository;
            this.logger = logger;
        }
        /// <summary>
        /// 生产任务删除
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<Result> Handle(DeleteProductionTasksCommand request, CancellationToken cancellationToken)
        {
            var result = new Result() { Code = (int)CommonErrorCode.Search };
            try
            {
                var list = await(await productiontasksrepository.GetAll()).Where(d => d.ProductiontasksId == request.ProductiontasksId).FirstAsync();
                if (list != null)
                {
                    list.IsDel = 2;
                }
                var res = productiontasksrepository.Update(list);
                result.Code = (int)CommonErrorCode.Success;
                result.Msg = "生产任务删除成功";
            }
            catch (Exception ex)
            {
                logger.LogError("生产任务删除异常");
                result.Msg = "生产任务删除异常";
            }
            return result;
        }
    }
}
